package com.wty.mianshi.circulation;

import com.wty.mianshi.common.Node;

import java.util.ArrayList;
import java.util.Arrays;

public class LinkedReverse {

    /**
     * 循环反转链表
     * @param head
     * @return
     */
    public Node reverseLinkedList(Node head){

        Node newHead=null;
        Node currentHead=head;

        while (currentHead!=null){
            Node next=currentHead.getNext();
            currentHead.setNext(newHead);
            newHead=currentHead;
            currentHead=next;
        }
        return newHead;
    }


    /**
     * 从链表的某一个节点开始打印链表各个节点存储的数据
     * @param node
     */
    public void printLinkedList(Node node){
        while (node!=null){
            System.out.print(node.getData());
            System.out.print(" ");
            node=node.getNext();
        }
        System.out.println();
    }






    public static void main(String args[]){
        LinkedCreator linkedCreator=new LinkedCreator();
        LinkedReverse linkedReverse=new LinkedReverse();

        Node node1 = linkedReverse.reverseLinkedList(linkedCreator.createLinkedList(new ArrayList()));
        linkedCreator.printLinkedList(node1);

        Node node2 = linkedReverse.reverseLinkedList(linkedCreator.createLinkedList(Arrays.asList(1)));
        linkedCreator.printLinkedList(node2);

        Node node3 = linkedReverse.reverseLinkedList(linkedCreator.createLinkedList(Arrays.asList(1, 2, 3, 4, 5)));
        linkedCreator.printLinkedList(node3);
    }
}
